package com.yjx.util;

import org.springframework.util.DigestUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class TokenUtil {
    //生成登录token
    //token:PC-LOGINNAME[加密]-CREATIONDATE-RONDEM[6 位]
    public static String createToken(String loginName) {
        //YYYYMMddHHmmss
        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYYMMddHHmmss");
        StringBuffer stringBuffer = new StringBuffer("token:PC-");
        stringBuffer.append(DigestUtils.md5Digest(loginName.getBytes()));
        stringBuffer.append("-");
        stringBuffer.append(simpleDateFormat.format(now));
        stringBuffer.append("-");
        String uuid = UUID.randomUUID().toString()
                .replaceAll("-", "");
        uuid = uuid.substring(uuid.length() - 6);
        stringBuffer.append(uuid);
        return stringBuffer.toString();
    }
}
